package com.fyw.magichome.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.fyw.magichome.BaseActivity;
import com.fyw.magichome.Constants;
import com.fyw.magichome.R;
import com.fyw.magichome.adapter.CameraListAdapter;
import com.fyw.magichome.bean.CameraList;
import com.fyw.magichome.http.ClientService;
import com.fyw.magichome.http.RetrofitManager;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by FYW on 2017/9/26.
 */

public class CameraListActivity extends BaseActivity {
    private ListView cameraListView;
    private List<CameraList.DataBean> dataBeanList;

    @Override
    public void initParam(Bundle param) {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_camera_list;
    }

    @Override
    public void initView(View view) {
        cameraListView = (ListView) findViewById(R.id.cameraListView);
        Call<CameraList> call = RetrofitManager.getInstance().createReq(ClientService.class)
                .getCameraList(Constants.accessTokenStr, 0, 50);
        call.enqueue(new Callback<CameraList>() {
            @Override
            public void onResponse(Call<CameraList> call, Response<CameraList> response) {
                if (response.body().getCode() == 200) {
                    dataBeanList = response.body().getData();
                    cameraListView.setAdapter(new CameraListAdapter(CameraListActivity.this, dataBeanList));
                } else
                    showToast(response.body().getMsg());
            }

            @Override
            public void onFailure(Call<CameraList> call, Throwable throwable) {
                showToast(throwable.getMessage());
            }
        });
    }

    @Override
    public void doBusiness(Context mContext) {
        cameraListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle bundle = new Bundle();
                bundle.putString("deviceSerial", dataBeanList.get(i).getDeviceSerial());
                bundle.putInt("channelNo", dataBeanList.get(i).getChannelNo());
                startActivity(CameraPlayActivity.class, bundle);
            }
        });
    }
}
